package me.boot.auth.jap.controller;

import com.fujieid.jap.core.config.AuthenticateConfig;
import com.fujieid.jap.oauth2.OAuthConfig;
import com.fujieid.jap.oauth2.Oauth2GrantType;
import me.boot.auth.jap.config.AuthProperties;
import me.boot.auth.jap.config.AuthStrategy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用第三方OAuth2.0服务之客户端凭证模式（适用于没有前端的命令行应用）
 * 需要依赖 jap-oauth2 模块
 */
@RestController
@RequestMapping("/oauth2/password")
public class Oauth2PasswordController extends BaseOAuth2Controller {

    @Override
    protected AuthStrategy getAuthStrategy() {
        return AuthStrategy.oauth2_password;
    }

    @Override
    protected AuthenticateConfig getAuthenticateConfig(AuthProperties.Platform platform) {
        OAuthConfig config = (OAuthConfig) super.getAuthenticateConfig(platform);

        // 密码模式，GrantType 设为 password
        config.setGrantType(Oauth2GrantType.PASSWORD);
        // 密码模式，不需要授权端链接
        config.setAuthorizationUrl(null);
        // 指定账号密码
        config.setUsername(platform.getUsername());
        config.setPassword(platform.getPassword());
        return config;
    }

}
